import axios from 'axios'
// 获取目录页右侧的内容
const CancelToken = axios.CancelToken
let cancel
// cancel the request

export const getContentInfo = (id) => {
  cancel && cancel('取消了前一次的请求！')
  const url = `http://www.imooc.com/api/category/content/${id}`
  return axios.get(url, {
    timeout: 30000,
    cancelToken: new CancelToken(function executor (c) {
    // An executor function receives a cancel function as a parameter
      cancel = c
    })
  })
    .then(res => {
      if (res.data) {
        return res.data.content
      }
      throw new Error('未能获取到数据')
    })
    .catch(err => {
      if (err) {
        console.log(err)
      }
    }
    )
}
